package com.cswangdao.controller.admin;


import com.cswangdao.domain.dto.GoodsAddDTO;
import com.cswangdao.domain.dto.GoodsPageDTO;
import com.cswangdao.domain.po.Goods;
import com.cswangdao.domain.vo.CatAndBrandVo;
import com.cswangdao.domain.vo.GoodsDetailVo;
import com.cswangdao.result.PageResult;
import com.cswangdao.result.Result;
import com.cswangdao.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RequestMapping("/admin/goods")
@RestController
@Slf4j
public class AdminGoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * @description: 分页查询商品列表
     * @param: request, response
     * @return: void
     */

    @GetMapping("/list")
    public Result<PageResult<Goods>> list(GoodsPageDTO pageInfo) {

        log.info("分页查询商品列表");
        PageResult<Goods> pageResult = goodsService.pageQueryInfo(pageInfo);
        return Result.success(pageResult);
    }

    /**
     * @description: 新增商品信息
     * @param: request, response
     * @return: void
     */
    @PostMapping("/create")
    public Result<Goods> create(@RequestBody GoodsAddDTO goodsAddDTO) {
        log.info("新增商品信息:{}", goodsAddDTO);
        boolean flag = goodsService.insert(goodsAddDTO);
        return flag ? Result.success(goodsAddDTO.getGoods()) : Result.error("新增失败");
    }

    /**
     * @description: 根据id删除商品信息
     * @param: request, response
     * @return: void
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody Goods goods) {
        log.info("根据id删除商品信息:{}", goods);
        boolean flag = goodsService.deleteById(goods.getId());
        return flag ? Result.success() : Result.error("删除失败");
    }

    /**
     * @description: 获取商品详情信息
     * @param: request, response
     * @return: void
     */
    @GetMapping("/detail")
    public Result<GoodsDetailVo> detail(Integer id) {
        log.info("获取商品详情信息:{}", id);
        GoodsDetailVo goodsDetailVo = goodsService.queryById(id);
        return Result.success(goodsDetailVo);
    }

    /**
     * @description: 查询商品品牌和目录
     * @param: request, response
     * @return: void
     */
    @GetMapping("/catAndBrand")
    public Result<CatAndBrandVo> catAndBrand() {
        log.info("查询商品品牌和目录");
        CatAndBrandVo catAndBrandVo = goodsService.getCatAndBrandList();
        return Result.success(catAndBrandVo);
    }

    /**
     * @description: 修改商品信息
     * @param: request, response
     * @return: void
     */
    @PostMapping("/update")
    public Result update(@RequestBody GoodsAddDTO goodsUpdate) {
        log.info("修改商品信息:{}", goodsUpdate);
        boolean flag = goodsService.update(goodsUpdate);
        return flag ? Result.success() : Result.error("修改失败");
    }
}
